package com.qyer.lxp.product.service.impl;

import com.alibaba.dubbo.rpc.protocol.rest.support.ContentType;
import com.google.gson.Gson;
import com.qyer.lxp.common.framework.contants.ClientErrorMessageEnum;
import com.qyer.lxp.common.framework.exception.QyerClientException;
import com.qyer.lxp.common.framework.util.QyerUtils;
import com.qyer.lxp.model.Response;
import com.qyer.lxp.model.price.normal.Price;
import com.qyer.lxp.model.product.lastminute.Lastminute_all;
import com.qyer.lxp.model.user.User;
import com.qyer.lxp.product.model.ProductParam;
import com.qyer.lxp.product.service.ITestService;
import com.qyer.lxp.service.product.IProductService;
import com.qyer.lxp.service.user.IUserService;

import javax.annotation.Resource;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;

/**
 * Created by jiangping on 16/5/24.
 */
@Path("/")
@Consumes({MediaType.APPLICATION_JSON, MediaType.TEXT_XML})
@Produces({ContentType.APPLICATION_JSON_UTF_8, ContentType.TEXT_XML_UTF_8})
public class TestServiceImpl implements ITestService {

    @Resource
    private IUserService userService;

    @Resource
    private IProductService productService;

    @GET
    @Path("/users/{id : \\d+}")
    public User getUser(@PathParam("id") Long id/*, @Context HttpServletRequest request*/) {
        return userService.getUser(id);
    }

    @GET
    @Path("/name/{name}")
    public String getName(@PathParam("name") String name/*, @Context HttpServletRequest request*/) throws Exception{
        if(QyerUtils.isNotEmpty(name)){
            throw new QyerClientException(ClientErrorMessageEnum.CLIENT_RESPONSE_400.getCode());
        }
        return "测试get方式的字符提交" + name;
    }
    @POST
    @Path("price")
    public String getObject(Price price) {
        Response<Price> response = new Response<Price>();
        response.setData(price);
        Gson gson = new Gson();
        String respone = gson.toJson(response).toString();
        return respone;
    }

    @POST
    @Path("priceTest")
    public Response<Price> getPriceResponse(Price price) {
        Response<Price> response = new Response<Price>();
        response.setNameSpace("priceTest");
        response.setData(price);
        return response;
    }

    @POST
    @Path("/productInfoTest")
    public Response<Lastminute_all> getProductInfo(ProductParam param) throws Exception{
        if(null==param){
            throw new QyerClientException(ClientErrorMessageEnum.CLIENT_RESPONSE_400.getCode());
        }
        Response<Lastminute_all> response = new Response<Lastminute_all>();
        Lastminute_all lastminute_all = productService.getInfoByProductCode(param.getProductCode());
        response.setNameSpace("productInfoTest");
        response.setData(lastminute_all);
        return response;
    }

}
